package likou.Thread;

import java.util.concurrent.atomic.AtomicInteger;
/**
 * @description:
 * @author: zp
 * @date: 2021/8/28 18:11
 * @param: null
 * @return:
 * 1114. 按序打印
 * 请设计修改程序，以确保 second() 方法在 first() 方法之后被执行，third() 方法在 second() 方法之后被执行。
 */
public class Foo {
    private AtomicInteger atomicInteger = new AtomicInteger(0);

    public void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        while (atomicInteger.get() != 0){

        }
        printFirst.run();
        atomicInteger.incrementAndGet();
    }

    public void second(Runnable printSecond) throws InterruptedException {

        // printSecond.run() outputs "second". Do not change or remove this line.
        while (atomicInteger.get() != 1){
        }
        printSecond.run();
        atomicInteger.incrementAndGet();
    }

    public void third(Runnable printThird) throws InterruptedException {

        // printThird.run() outputs "third". Do not change or remove this line.
        while (atomicInteger.get() != 2){
        }
        printThird.run();
        atomicInteger.incrementAndGet();
    }
}
